

#include "MyDefinitions.h"
#include "MyUniversal.h"
#include "MyInfobar.h"
#include "MyGame.h"
#include "MyPlayer.h"
#include "MyEngine.h"
#include "MyPlayerActionManager.h"

MyInfobar::MyInfobar(MyGame *_parent)
{ 
	parent = _parent;	
	wasInit = false;
	enemies_killed_text[0] = '0';
	enemies_killed_text[1] = 0;
	money_text[0] = '0';
	money_text[1] = 0;
}
MyInfobar::~MyInfobar()
{
	if(wasInit)
	{
		unload_surface(infobarSheet);
		unload_surface(infobarSheetBkp);
		unload_surface(topInfobarSheet);
		#ifdef _DEBUG
		printf("destroyed infobar sheets\n");
		#endif
	}
}
void MyInfobar::update_pools_info()
{
	//safety switch
	if (!parent->parent->is_running()) parent->demand_quit();

	life_displayed_cur = parent->actPlayer->get_cur_life();
	life_displayed_max = parent->actPlayer->get_max_life();
	mana_displayed_cur = parent->actPlayer->get_cur_mana();
	mana_displayed_max = parent->actPlayer->get_max_mana();
}

void MyInfobar::make_infobar()
{	//safety switch
	if (!parent->parent->is_running())  parent->demand_quit();
	SDL_Rect *tmp, *tmp1;
	SDL_Surface *tmp2;
	//start with temp infobar surface
	SDL_BlitSurface(infobarSheetBkp, 0,infobarSheet,0);
	//draw the weapon image on it's place on the infobar
	SDL_BlitSurface(parent->actPlayerActionManager->actionImgSheet, tmp1 = parent->actPlayerActionManager->getActShotRect(), infobarSheet, tmp = make_rect(620, 10, 40, 40));
	delete(tmp);
	delete(tmp1);
	tmp1 = parent->actPlayerActionManager->getActSpellRect();
	if (tmp1) //in case no spell yet
	{
		SDL_BlitSurface(parent->actPlayerActionManager->actionImgSheet, tmp1, infobarSheet, tmp = make_rect(740, 10, 40, 40));
		delete(tmp);
		delete(tmp1);
	}
	//make bar for life - max = 100% //80,10,180,24
 	SDL_FillRect(infobarSheet, tmp=make_rect(55,17,129,8), SDL_MapRGB( infobarSheet->format, parent->color_life2->r,parent->color_life2->g,parent->color_life2->b));       
	delete(tmp);
	SDL_FillRect(infobarSheet, tmp=make_rect(55,17,(129*parent->actPlayer->get_cur_life())/parent->actPlayer->get_max_life(),8), 
		SDL_MapRGB( (parent->parent->screen->format), parent->color_life1->r,parent->color_life1->g,parent->color_life1->b));       
	delete(tmp);
	//make bar for mana - max = 100% //80,10,180,24
	SDL_FillRect(infobarSheet, tmp=make_rect(55,38,129,8), 
		SDL_MapRGB( (parent->parent->screen->format), parent->color_mana2->r,parent->color_mana2->g,parent->color_mana2->b));       	
	delete(tmp);
	SDL_FillRect(infobarSheet, tmp=make_rect(55,38,(129*parent->actPlayer->get_cur_mana()/parent->actPlayer->get_max_mana()),8), 
		SDL_MapRGB( (parent->parent->screen->format),parent->color_mana1->r,parent->color_mana1->g,parent->color_mana1->b));       	
	delete(tmp);
	//make bar for exp //10,10,600,5	
	SDL_FillRect(infobarSheet, tmp=make_rect(0,4,parent->actPlayer->get_exp_line_size(), 2), 
		SDL_MapRGB( (parent->parent->screen->format), parent->color_exp1->r,parent->color_exp1->g,parent->color_exp1->b));       	
	delete(tmp);	
	//write money on infobar
	tmp2 = TTF_RenderText_Blended(parent->parent->font, money_text, *(parent->color_exp1));
	SDL_BlitSurface(tmp2, 0,infobarSheet,tmp1=make_rect(260,15,tmp2->w, tmp2->h));
	delete(tmp1);
	unload_surface(tmp2);
	//write enemy on infobar
	tmp2 = TTF_RenderText_Blended(parent->parent->font, enemies_killed_text, *(parent->color_white));
	SDL_BlitSurface(tmp2, 0,infobarSheet,tmp1=make_rect(260,35,tmp2->w, tmp2->h));
	delete(tmp1);
	unload_surface(tmp2);
}
//this is the function that will ensure infobar is actualized and displayed
void MyInfobar::draw_infobar()
{	
	SDL_Rect *tmp;
	//actualize the info displayed
	update_pools_info();
	//make the infobar to be displayed
	make_infobar();		
	//cover the old infobar - did for text msgs
	SDL_BlitSurface(infobarSheetBkp, 0, parent->parent->screen,tmp=make_rect(INFOBAR_X,INFOBAR_Y,INFOBAR_W,INFOBAR_H));
	delete(tmp);
	//display infobar on screen	
	SDL_BlitSurface(infobarSheet, 0, parent->parent->screen,tmp=make_rect(INFOBAR_X,INFOBAR_Y,INFOBAR_W,INFOBAR_H));
	delete(tmp);	
	//display top msg
	SDL_BlitSurface(topInfobarSheet, 0, parent->parent->screen,0);	
}
void MyInfobar::make_top_msg(const char* _message)
{
	unload_surface(topInfobarSheet);
	//generate message from char*	
	topInfobarSheet = TTF_RenderText_Blended (parent->parent->font, _message, *(parent->color_white));
}
//loads the images from disk, stores the infobar to the infobarSheetBkp
void MyInfobar::load_imgs()
{
	#ifdef _WIN32
	infobarSheetBkp = load_image("misc\\infobar.png");
	infobarSheet = load_image("misc\\infobar.png");
	#else 
	infobarSheetBkp = load_image("misc/infobar.png");
	infobarSheet = load_image("misc/infobar.png");
	#endif
	topInfobarSheet = TTF_RenderText_Blended (parent->parent->font, "Hello", *(parent->color_white));

	if ((!infobarSheetBkp) || (!infobarSheet))
	{	
			printf("ERROR: Cannot load game images!\n (infobar.png missing?");
			parent->parent->goto_gs(ES_QUIT);
	}	    
}